﻿/**
 *	html/monitor/center/scene/panel_alarm.js
 *
 *	(C) 2014 Yuanjj
 *
 * 	history:
 * 		2014-07-20			create the file
 */

/**
 *	create the alarm panel
 *
 *	@panel_center		[in] the center panel object
 *	@showflag			[in] the show flag ( "show" or "hide" )
 *
 *	return
 *		void
 */
function create_alarm_panel( panel_center, showflag )
{
    var panel = new Ext.panel.Panel
    ({
         id:"panel_c_alarm",
         layout:"vbox",
         flex:1,
         width:"100%",
         listeners:{ afterrender:_create_alarm_component }
    });

    if ( "show" === showflag )
        panel.show();
    else
        panel.hide();

    panel_center.add_subpanel( panel );
}

/**
 *	create the components for alarm
 *
 *	@panel_c_alarm			[in] the alarm panel object
 */
function _create_alarm_component( panel_c_alarm )
{
    var alarm_top_bar	= _create_alarm_top_bar();
    var alarm_grid		= _create_alarm_grid();

    panel_c_alarm.add( alarm_top_bar );
    panel_c_alarm.add( alarm_grid );
}

/**
 *	create the toolbar for alarm panel
 *
 *	return
 *		the toolbar object
 */
function _create_alarm_top_bar()
{
    var alarm_top_bar = Ext.create
    ("Ext.toolbar.Toolbar",{ id:"alarm_top_bar", width:"100%", height:30, });

    var top_bar_combox 			= create_form_field_combobox( alarm_top_bar, "通道类型", 50, 150, g_tunnel_type, "type", "local", 60, "serial", 0 )
    var top_bar_search_button 	= create_form_field_button( alarm_top_bar, "查询", on_search_alarm );

    alarm_top_bar.add(
        { xtype:"tbspacer", width:10 },
        top_bar_combox,
        { xtype:"tbspacer", width:10 },
        top_bar_search_button,
        "->",
        { xtype:"button", text:"添加", handler:on_add_new_alarm },
        "-",
        { xtype:"button", text:"修改" },
        "-",
        { xtype:"button", text:"删除" }
    );

    alarm_top_bar.search_combobox	= top_bar_combox;
    alarm_top_bar.search_button		= top_bar_search_button;

    return alarm_top_bar;
}

/**
 *	create the grid for alarm panel
 *
 *	return
 *		the grid object
 */
function _create_alarm_grid()
{
    var alarm_grid = new Ext.grid.Panel
    ({
         id:"alarm_grid",
         width:"100%",
         flex:1,
         store:g_alarm_linkage,
         columns:[
             {header:"序号", width:50, xtype:"rownumberer"},
             {header:"主机编号", width:100, dataIndex:"host", align:"center" },
             {header:"通道类型", width:100, align:"center", renderer:render_tunnel_type },
             {header:"通道编号", width:100, align:"center", renderer:render_tunnel_num },
             {header:"接入设备", width:100, align:"center", renderer:render_device_name },
             {header:"设备点号", width:100, dataIndex:"dev_id", align:"center" },
             {header:"报警条件", width:100, align:"center", renderer:render_alarm_start },
             {header:"停止条件", width:100, align:"center", renderer:render_alarm_stop },
             {header:"联动主机", width:100, dataIndex:"link_host", align:"center"},
             {header:"联动设备", width:100, align:"center", renderer:render_linkage_device }
         ]
    });

    return alarm_grid;
}

/**
 *	renderer for tunnel type
 *
 *	@v			[in] the value of store field
 *	@meta		[in] cell metaData object
 *	@rec		[in] store record object
 *
 *	return
 *		text to be shown
 */
function render_tunnel_type( v, meta, rec )
{
    var	device	= g_device.findRecord( "dev_id", rec.data.dev_id );
    if ( null === device )
        return "";

    return device.get( "tunnel_type" );
}

/**
 *	renderer for tunnel number
 *
 *	@v			[in] the value of store field
 *	@meta		[in] cell metaData object
 *	@rec		[in] store record object
 *
 *	return
 *		text to be shown
 */
function render_tunnel_num( v, meta, rec )
{
    var	device	= g_device.findRecord( "dev_id", rec.data.dev_id );
    if ( null === device )
        return "";

    return device.get( "tunnel_num" );
}

/**
 *	renderer for device name
 *
 *	@v			[in] the value of store field
 *	@meta		[in] cell metaData object
 *	@rec		[in] store record object
 *
 *	return
 *		text to be shown
 */
function render_device_name( v, meta, rec )
{
    var	device	= g_device.findRecord( "dev_id", rec.data.dev_id );
    if ( null === device )
        return "";

    return device.get( "dev_name" );
}

/**
 *	renderer for alarm start
 *
 *	@v			[in] the value of store field
 *	@meta		[in] cell metaData object
 *	@rec		[in] store record object
 *
 *	return
 *		new text to be shown
 */
function render_alarm_start( v, meta, rec )
{
    if ( "" === rec.data.alm_start )
        return "";

    var	dev_record	= g_device.findRecord( "dev_id", rec.data.dev_id );
    if ( null == dev_record )
        return "";

    var	dev_type	= g_device_type.findRecord( "type", dev_record.get( "dev_type" ) );
    if ( null == dev_type )
        return "";

    var	name		= dev_type.get( "name" );
    var text 		= name + " " + rec.data.cmp_start + " " + rec.data.alm_start;

    return text;
}

/**
 *	renderer for alarm stop
 *
 *	@v			[in] the value of store field
 *	@meta		[in] cell metaData object
 *	@rec		[in] store record object
 *
 *	return
 *		new text to be shown
 */
function render_alarm_stop( v, meta, rec )
{
    if ( "" === rec.data.alm_stop )
        return "";

    var	dev_record	= g_device.findRecord( "dev_id", rec.data.dev_id );
    if ( null == dev_record )
        return "";

    var	dev_type	= g_device_type.findRecord( "type", dev_record.get( "dev_type" ) );
    if ( null == dev_type )
        return "";

    var	name		= dev_type.get( "name" );
    var text 		= name + " " + rec.data.cmp_stop + " " + rec.data.alm_stop;

    return text;
}

/**
 *	renderer for linkage device
 *
 *	@v			[in] the value of store field
 *	@meta		[in] cell metaData object
 *	@rec		[in] store record object
 *
 *	return
 *		new text to be shown
 */
function render_linkage_device( v, meta, rec )
{
    if ( "" === rec.data.link_dev )
        return "";

    var device = g_device.findRecord( "dev_id", rec.data.link_dev )
    if ( null === device )
        return "";

    return device.get( "dev_name" );
}

/**
 *	search tunnel type button handler
 */
function on_search_alarm()
{
    var tunneltype 		= this.parent.search_combobox.getRawValue();
    g_alarm_linkage.filterBy( function( record )
    {
        if ( "全部" === tunneltype )
            return true;
        else
            return (record.get( "tunnel_type" ) === tunneltype);
    });
}

/**
 *	add new alarm
 */
var g_form_alarm;
function on_add_new_alarm()
{

}
